package com.example.shushiyunsst.fragment.main.home.child.fu_li_pool.sub

import android.os.Bundle
import android.view.View
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.adapter.JssBaseViewHolder
import com.example.library_common.base.SimpleListFragment
import com.example.library_common.bean.CommonBean
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.main.home.child.fu_li_pool.bean.FuLiPoolHelperBean
import com.example.shushiyunsst.fragment.main.home.child.miner22.Miner22HelperFragment
import com.example.shushiyunsst.fragment.main.home.child.miner22.bean.Miner22HelperBean
import ikidou.reflect.TypeBuilder
import java.lang.reflect.Type

class FuLiPoolHelperFragment : SimpleListFragment<FuLiPoolHelperBean>() {

    companion object {
        fun newInstance(): FuLiPoolHelperFragment {
            val args = Bundle()
            val fragment = FuLiPoolHelperFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        isAddDividerItemDecoration = false
        isEnableLoadMore=false
    }

    override fun getItemLayout(): Int {
         return R.layout.fu_li_pool_helper_fragment
    }

    override fun initView(view: View) {
        super.initView(view)
        view.setBackgroundResource(R.color.bg_color)
        mHeader.removeAllViews()
        addHeader(R.layout.comment_header_layout)
        mHeader.setBackgroundResource(R.color.bg_color_0)
        val mToolbar: Toolbar = mHeader.findViewById(R.id.mToolbar)
        mToolbar.title = "攻略"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
    }

    override fun convertItem(helper: JssBaseViewHolder?, item: FuLiPoolHelperBean?) {
        helper?.setText(R.id.title, item?.title)
            ?.setText(R.id.content, item?.content)
            ?.setImageNetUrl(R.id.icon, item?.logo)
    }

    override fun getListType(): Type {
        return TypeBuilder.newInstance(CommonBean::class.java)
            .beginSubType(List::class.java)
            .addTypeParam(FuLiPoolHelperBean::class.java).endSubType().build()
    }

    override fun netRequest() {
        val map = mapOf(
            "htype" to 1,

            )
        RestClient.builder()
            .url(R.string.queryHelps)
            .raw(JSON.toJSONString(map))
            .success {
                parseDate(it)
            }
            .failure { onFailed() }
            .error { _, _ -> onFailed() }
            .build().post()
    }

    override fun parseDate(data: String?) {
//        super.parseDate(data)
        val obj = JSON.parseObject(data)
        val helpList = obj["helpList"].toString()
        val array = JSON.parseArray(helpList, FuLiPoolHelperBean::class.java)
        loadListDate(array)
    }

}